package com.dr.net.upLoadPic;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;

public class UpLoadPicClient {

    public static void main(String[] args) {
        Socket socket = null;
        BufferedOutputStream outputPic = null;
        BufferedInputStream loadPic = null;
        try {
            socket = new Socket("localhost", 40000);

            outputPic = new BufferedOutputStream(socket.getOutputStream());
            loadPic = new BufferedInputStream(new FileInputStream("timg.jpg"));

            byte[] buf = new byte[1024];
            int len;
            while ((len = loadPic.read(buf)) != -1) {
                outputPic.write(buf, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outputPic != null) {
                try {
                    outputPic.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (loadPic != null) {
                try {
                    loadPic.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

package com.dr.net.upLoadPic;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class UpLoadPicServer {

    public static void main(String[] args) {
        ServerSocket server = null;

        try {
            server = new ServerSocket(40000);
            while (true) {
                Socket socket = server.accept();
                new Thread(new Uploader(socket)).start();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (server != null) {
                try {
                    server.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


package com.dr.net.upLoadPic;

import java.io.*;
import java.net.Socket;

public class Uploader implements Runnable {
    private String ip;
    private Socket socket;

    public Uploader(Socket socket) {
        this.ip = socket.getInetAddress().getHostAddress();
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedInputStream downloadPic = null;
        BufferedOutputStream savePic = null;

        File file = new File(ip + "jpg");
        int count = 1;
        while (file.exists()) {
            file = new File(ip + "(" + count + ").jpg");
        }

        try {
            downloadPic = new BufferedInputStream(socket.getInputStream());
            savePic = new BufferedOutputStream(new FileOutputStream(file));

            byte[] buf = new byte[1024];
            int len;
            while ((len = downloadPic.read(buf)) != -1) {
                savePic.write(buf, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (downloadPic != null) {
                try {
                    downloadPic.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (savePic != null) {
                try {
                    savePic.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
